<template>
  <el-dialog :visible.sync="visible" width="36%" :before-close="onClose">
    <div slot="title" style="font-size: 14px;font-weight: 500;">
      添加小程序
    </div>
    <div class="add-min-container">
      <el-form :label-position="'right'" ref="form" :model="form" label-width="100px">
        <el-form-item label="小程序名称：">
          <el-input type="text" placeholder="填写后生成常用小程序，之后可快速选择" v-model="form.name">
          </el-input>
        </el-form-item>
        <el-form-item label="小程序ID：">
          <el-input type="text" placeholder="请输入小程序ID" v-model="form.miniId">
          </el-input>
          <div style="color: rgba(0,0,0,.45);">小程序必须关联企业微信，否则将导致发送失败。<a style="color: #2f54eb;">如何关联小程序？</a></div>
        </el-form-item>
        <el-form-item label="小程序路径：">
          <el-input type="text" placeholder="pages/home/main" v-model="form.path">
          </el-input>
        </el-form-item>
        <el-form-item label="小程序标题：">
          <el-input type="text" placeholder="请先输入小程序标题，最多20个字" v-model="form.title" maxlength="20" show-word-limit>
          </el-input>
        </el-form-item>
        <el-form-item label="封面：">
          <el-upload class="avatar-uploader" :show-file-list="false" :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.imageUrl" :src="form.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip">图片不大于2M</div>
          </el-upload>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onClose">取消</el-button>
      <el-button type="primary" @click="onClose">保存</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      form: {
        title: '',
        path: '',
        imageUrl: ''
      }
    }
  },
  props: ['visible'],
  methods: {
    onClose() {
      this.$emit('handCloseDialog');
    },
    handleAvatarSuccess(res, file) {
      this.form.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  },
}
</script>
<style lang="scss" scoped>
.add-min-container {
  padding: 0 26px;

  ::v-deep .el-form--label-left .el-form-item__label {
    text-align: right;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 104px;
    height: 104px;
    line-height: 104px;
    text-align: center;
    border: 1px dashed #e9e9e9;
  }

  .avatar {
    width: 104px;
    height: 104px;
    display: block;
  }
}
</style>